package com.selftaught.login.controller;

import com.selftaught.login.common.BaseResponse;
import com.selftaught.login.model.dto.user.UserLoginRequest;
import com.selftaught.login.model.dto.user.UserRegisterRequest;
import com.selftaught.login.model.entity.User;
import com.selftaught.login.model.vo.UserVO;
import com.selftaught.login.service.UserService;
import com.selftaught.login.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制器
 *
 * @author <a href="https://gitee.com/wanghuya">程序员虎牙</a>
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 注册
     *
     * @param userRegisterRequest userRegisterRequest
     * @return 结果
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        return ResultUtils.success(userService.register(userRegisterRequest));
    }

    /**
     * 登录
     *
     * @param userLoginRequest userLoginRequest
     * @param request          request
     * @return 结果
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        User loginUser = userService.login(userLoginRequest, request);
        return ResultUtils.success(userService.getUserVo(loginUser));
    }

    /**
     * 退出登录
     *
     * @param request request
     * @return 结果
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        return ResultUtils.success(userService.logout(request));
    }

    /**
     * 获取当前登录用户
     *
     * @param request request
     * @return 结果
     */
    @GetMapping("/get/current")
    public BaseResponse<UserVO> getLoginUser(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return ResultUtils.success(userService.getUserVo(user));
    }
}
